package com.zcy.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Objects;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener {
    int[][] data = new int[4][4];
    String path = "day-code1103\\src\\com\\zcy\\ui\\image\\animal\\animal3\\";
    String winImgPath = "day-code1103\\src\\com\\zcy\\ui\\image\\win.png";
    int[][] win = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    int count = 0;
    JMenuBar jMenuBarFn = new JMenuBarFn();
//    ActionListener


    public GameJFrame(int width, int height, String title) {
        // 用来管理数据
        // 加载图片的时候是通过二维数组中的数据进行加载的
        initJFrame(width, height, title);

        //设置菜单栏
        this.setJMenuBar(jMenuBarFn);

        //初始化数据（打乱拼图数据）
        initData();

        //初始化图片
        initImage();

        this.setVisible(true);
    }

    public void initJFrame(int width, int height, String title) {
        this.setSize(width, height);
        this.setTitle(title);
//        this.setAlwaysOnTop(true);//置顶
//        this.setLocationRelativeTo(null);//界面居中
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口
        this.setLayout(null);//设置布局为空
        //给整个界面添加键盘监听事件
        this.addKeyListener(this);
    }

    // 初始化图片
    public void initImage() {

        this.getContentPane().removeAll();

        if (victory()) {
            JOptionPane.showMessageDialog(this, "恭喜你，拼图成功！");
            JLabel label = new JLabel(new ImageIcon(winImgPath));
            label.setBounds(203, 283, 197, 73);
            this.add(label);
        }

        JLabel stepCount = new JLabel("步数：" + count);
        stepCount.setBounds(50, 30, 100, 20);
        this.add(stepCount);
        //number自增 每次添加图片后增加下一张图片
//        int nummber = 1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                ImageIcon icon1 = new ImageIcon(path + (data[i][j]) + ".jpg");
                JLabel label = new JLabel(icon1);
                label.setBounds(j * 105 + 83, i * 105 + 134, 105, 105);
                label.setBorder(new BevelBorder(1));// 0图片凸起来 1图片凹下去
                this.add(label);
//                nummber++;
            }
        }

        ImageIcon imageIcon = new ImageIcon("day-code1103\\src\\com\\zcy\\ui\\image\\background.png");
        JLabel imageLabel = new JLabel(imageIcon);
        imageLabel.setBounds(40, 40, 508, 560);
        this.add(imageLabel);

        this.getContentPane().repaint();
    }

    //记录空白方块在二维数组中的位置
    int x = 0;
    int y = 0;

    // 初始化数据（打乱拼图方法）
    public void initData() {
        int[] template = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random r = new Random();
        for (int i = 0; i < template.length; i++) {
            int index = r.nextInt(template.length);
            int temp = template[i];
            template[i] = template[index];
            template[index] = temp;
        }
        // 创建一个二维数组
        for (int i = 0; i < template.length; i++) {
            if (template[i] == 0) {
                x = i / 4;
                y = i % 4;
                System.out.println(x + "-x");
                System.out.println(y + "-y");
            }
            data[i / 4][i % 4] = template[i];
        }
    }

    // 键盘输入事件
    @Override
    public void keyTyped(KeyEvent e) {

    }

    // 键盘按下事件
    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == 65) {
            System.out.println("A键按住不松");
            //删除容器中所有图片
            this.getContentPane().removeAll();
            JLabel label = new JLabel(new ImageIcon(path + "all.jpg"));
            label.setBounds(40, 40, 508, 560);
            this.getContentPane().add(label);

            //添加背景色图片
            ImageIcon imageIcon = new ImageIcon("day-code1103\\src\\com\\zcy\\ui\\image\\background.png");
            JLabel imageLabel = new JLabel(imageIcon);
            imageLabel.setBounds(40, 40, 508, 560);
            this.add(imageLabel);
            this.getContentPane().repaint();

        }
    }

    // 键盘释放事件
    @Override
    public void keyReleased(KeyEvent e) {
        if (victory()) {
            return;
        }
        int code = e.getKeyCode();
        switch (code) {
            case KeyEvent.VK_UP:
                System.out.println("向上");
                if (x < 3) {
                    data[x][y] = data[x + 1][y];
                    data[x + 1][y] = 0;
                    x++;
                    this.count++;
                    this.initImage();
                }
                break;
            case KeyEvent.VK_DOWN:
                if (x > 0) {
                    data[x][y] = data[x - 1][y];
                    data[x - 1][y] = 0;
                    x--;
                    this.count++;
                    this.initImage();
                }
                break;
            case KeyEvent.VK_LEFT:
                if (y < 3) {
                    System.out.println("向右");
                    data[x][y] = data[x][y + 1];
                    data[x][y + 1] = 0;
                    y++;
                    this.count++;
                    this.initImage();
                }

                break;
            case KeyEvent.VK_RIGHT:
                if (y > 0) {
                    System.out.println("向左");
                    data[x][y] = data[x][y - 1];
                    data[x][y - 1] = 0;
                    y--;
                    this.count++;
                    this.initImage();
                }
                break;
            // A 键盘松开事件
            case 65:
                this.initImage();
                break;
            case 87:
                data = new int[][]{
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                        {13, 14, 15, 0}
                };
                this.x = 3;
                this.y = 3;
                this.initImage();
                break;
        }
    }

    public boolean victory() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }
}
